import re
import rtoml
from urllib.parse import urlparse
import tldextract


def get_main_url(url):
    url = url.split('#')[0]
    m = re.search(r'(http.*://[\w.-]*?)/', url)
    if not m:
        m = re.search(r'(http.*://[\w.-]*)', url)

    if m:
        return m.group(1)
    return url.split()[0]


def get_book_url(book_dict):
    whole_url = book_dict['bookSourceUrl']
    main_url = get_main_url(whole_url)
    if 'http' not in main_url:
        if 'searchUrl' in book_dict and 'http' in book_dict['searchUrl']:
            main_url = get_main_url(book_dict['searchUrl'])
    return main_url


def get_book_domain(book_dict):
    url = get_book_url(book_dict)
    if 'http' in url:
        ext = tldextract.extract(url)
        domain = ext.registered_domain
        return domain
    return url


uncheck_keys = ['bookSourceComment', 'bookSourceGroup', 'bookSourceName', 'bookSourceUrl',
                'customOrder', 'enabled', 'enabledCookieJar', 'enabledExplore', 'lastUpdateTime',
                'respondTime', 'weight']


def book_equal(book0, book1):
    if len(book0) != len(book1):
        return False
    else:
        for key in book0:
            if key in uncheck_keys:
                continue
            if book0.get(key) != book1.get(key):
                return False
    return True


def book_approximate(book0, book1):
    keys = ['searchUrl', 'exploreUrl']
    for key in keys:
        if book0.get(key) != book1.get(key):
            return False
    return True


def books_contain(blist, book):
    for b in blist:
        if book_equal(b, book):
            return True
    return False


def part_str_in(src_str, part_list):
    for part in part_list:
        if part in src_str:
            return True
    return False


def map_keyword_group(src_books):
    toml_path = 'keyword_groups.toml'
    with open(toml_path, 'r', encoding='utf-8') as f:
        map_dict = rtoml.load(f)

    for book in src_books:
        for group in map_dict:
            if part_str_in(book['bookSourceName'], map_dict[group]):
                if not book.get('bookSourceGroup') or "fox" not in book['bookSourceGroup']:
                    book['bookSourceGroup'] = group
                elif group not in book['bookSourceGroup']:
                    book['bookSourceGroup'] += ',{}'.format(group)


def map_exact_group(src_books):
    toml_path = 'exact_groups.toml'
    with open(toml_path, 'r', encoding='utf-8') as f:
        map_dict = rtoml.load(f)

    for book in src_books:
        for group in map_dict:
            if book['bookSourceUrl'] in map_dict[group]:
                if not book.get('bookSourceGroup') or "fox" not in book['bookSourceGroup']:
                    book['bookSourceGroup'] = group
                elif group not in book['bookSourceGroup']:
                    book['bookSourceGroup'] += ',{}'.format(group)


def get_same_domain_books(blist):
    gdict = {}

    for book in blist:
        cur_url = get_book_domain(book)
        if cur_url in gdict:
            gdict[cur_url].append(book)
        else:
            gdict[cur_url] = [book]

    return gdict


def book_has_search(book):
    return book.get('ruleSearch') and book['ruleSearch'].get('bookUrl') and book.get('searchUrl')


def book_has_explore(book):
    return book.get('ruleSearch') and book['ruleSearch'].get('bookUrl') and book.get('exploreUrl')
